<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__merge">
      <wms-form v-model="dataForm" :formList="formList" :table-column="tableColumn" :advanced="advanced"
        @check-change="checkChange" @changeAdvance="changeAdvance" @getDataList="getDataList" @getReset="getReset">
        <el-form-item label="需求站台">
          <el-select v-model="toStn">
            <el-option value="1F_IN_ST11">1F_IN_ST11</el-option>
            <el-option value="1F_OUT_ST11">1F_OUT_ST11</el-option>
            <el-option value="1F_OUT_ST12">1F_OUT_ST12</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateHandle()">{{
            '托盘呼叫'
          }}</el-button>
        </el-form-item>
      </wms-form>
      <wms-table ref="wmsTable" class="mergeTable" v-model="dataForm" :page="page" :total="total" :limit="limit"
        :tableData="dataList" :table-column="column" :module-options="mixinViewModuleOptions"
        :dataListLoading="dataListLoading" :showSelection="true" @selectChangeHandle="dataListSelectionChangeHandle"
        @pageSizeChangeHandle="pageSizeChangeHandle" @pageCurrentChangeHandle="pageCurrentChangeHandle"
        @changeLockSt="changeLockSt" />
         <!-- 弹窗, 新增 / 修改 -->
      <add-or-update :toStn="toStn" v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./components/merge-add-or-update";
import debounce from "lodash/debounce";
import { formList, tableColumn } from './config'
import wmsTable from '@/components/wms-table'
import wmsForm from '@/components/wms-form'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/task/page",
        getDataListIsPage: true,
        exportURL: "/wms/merge/export",
        deleteURL: "/wms/merge",
        deleteIsBatch: true,
        deleteIsBatchKey: 'mergeId'
      },
      dataForm: {
        taskSubType: '023',
        spuNo: '',
        spuName: '',
        contNo: '',
        curPosition: ''
      },
      changeForm: {
        mergeId: '',
        lockSt: ''
      },
      formList,
      tableColumn,
      column: [],
      advanced: false,
      toStn: ''
    };
  },
  components: {
    AddOrUpdate,
    wmsTable,
    wmsForm
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
    },
    // 托盘呼叫
    addOrUpdateHandle(id) {
      if(!this.toStn) {
        return this.$message({
          message: '请先选择需求站台！',
          type: 'warning',
          duration: 1000
        })
      }
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = id
        this.$refs.addOrUpdate.init()
      })
    },
    dataListSelectionChangeHandle(selection, row) {
      this.dataForm.mergeId = ''; //每次单选都重置id
      if (selection.length > 1) {
        this.$refs['wmsTable'].$refs.Table.clearSelection()
        this.$refs['wmsTable'].$refs.Table.toggleRowSelection(row)
      }
      if (selection.length != 0) {
        //只有当真正选中了目标主单才获取id
        this.dataForm.mergeId = row.mergeId;
      }
    },
    //1.库存冻结
    changeLockSt: debounce(function (val, row) {
      var tempStatus = row.lockSt === 0 ? 1 : 0;
      this.changeForm.mergeId = row.mergeId;
      this.changeForm.lockSt = row.lockSt;
      this.$http[!this.changeForm.mergeId ? 'post' : 'put']('/wms/merge/', this.changeForm).then(({ data: res }) => {
        if (res.code !== 0) {
          //如果更新失败则重置为原来的状态
          row.lockSt = tempStatus;
          return this.$message.error(res.msg)
        }
        if (this.changeForm.lockSt === 1) {
          this.$message({
            message: this.$t('prompt.freezed'),
            type: 'success',
            duration: 1000,
            onClose: () => {
            }
          })
        }
        if (this.changeForm.lockSt === 0) {
          this.$message({
            message: this.$t('prompt.thaw'),
            type: 'success',
            duration: 1000,
            onClose: () => {
            }
          })
        }
      }).catch(() => { })
    }),
    freezeHandle(id) {
      if (!this.dataForm.mergeId) {
        return this.$message({
          message: this.$t('prompt.freeze'),
          type: 'warning',
          duration: 1000
        })
      }
      this.$confirm(this.$t('prompt.info', { 'handle': this.$t('freeze') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post('/wms/merge/freeze/' + this.dataForm.mergeId)
          .then(({ data: res }) => {
            if (res.code != 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.getDataList()
              }
            })
          }).catch(() => { })


      }).catch(() => { })

    }
  }
}
</script>
<style scoped lang="scss">
/* 隐藏表头的全选框 */
::v-deep .mergeTable thead .el-table-column--selection .cell {
  display: none;
}
</style>
